<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dev_checklist extends MX_Controller {
    
  function __construct() {  
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('mobile_detect');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('dev_checklist/mdev_checklist');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
  }
  public function index(){
    $list = $this->mdev_checklist->get_outlet();
    $this->template->title('Sistem', "Outlet");
    
    $view = "outlet";
    if($this->mobile_detect->isMobile()){
      $view = "mobile/m-milik";
    }
    
    $this->template->build($view, 
      array('message' => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "development",
            'title_table'    => "Outlet Data Table",
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  function detail($id){
    $detail = $this->global_models->get("m_outlet", array("id_outlet"  =>  $id));
    
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('step-detail', 
        array(
              'message'     => "",
              'url'     => base_url()."themes/srabon/",
              'detail'    => $detail,
              'menu_drop'   => $this->menu,
              'menu'        => "outlet",
              'title_table'    => "Step ".$detail[0]->name,
              'foot'    => ""
            ));
      $this->template
        ->set_layout('default')
        ->build('step-detail');
    }
    else{
//      $this->debug($this->input->post(NULL, TRUE), true);
      $pst = $this->input->post(NULL, TRUE);
      $no = 0;
      
    }
  }
  function report($id, $id_detail = 0){
    $outlet = $this->global_models->get("m_outlet", array("id_outlet" => $id));
    $query = "
      SELECT D.*
      FROM `d_outlet_users` as A
      LEFT JOIN d_user_privilege as B ON A.id_users = B.id_users
      LEFT JOIN m_privilege as C ON B.id_privilege = C.id_privilege
      LEFT JOIN m_users as D ON A.id_users = D.id_users
      WHERE A.id_outlet = 5 AND LOWER(C.name) LIKE '%owner outlet%'
      ";
    $owner = $this->global_models->get_query($query);
    $query2 = "
      SELECT *
      FROM `m_dev_checklist`
      ORDER BY urut ASC
      ";
    $master_step = $this->global_models->get_query($query2);
    $line = array();
    $dev = $this->global_models->get("d_outlet_dev", array("id_outlet" => $id));
    
    $edit = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "dev_timeline", "edit");
    $add = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "dev_timeline", "add");
    
//    $this->debug($dev, true);
    if($dev AND $id_detail == 0 AND ($edit !== FALSE OR $add !== FALSE OR $this->session->userdata("id") == 1)){
      
      foreach ($master_step as $ms){
        $line[$ms->id_dev_step] = $this->global_models->get("d_dev_check", array("id_outlet_dev" => $dev[0]->id_outlet_dev, "id_dev_step" => $ms->id_dev_step));
      }
  //    $this->debug($dev, true);
      $this->template->build('step-detail', 
        array(
              'message'     => "",
              'url'     => base_url()."themes/srabon/",
              'detail'    => $outlet,
              'owner'    => $owner,
              'master'    => $master_step,
              'dev'    => $dev,
              'edit'  =>  $edit,
              'add'  =>  $add,
              'line'    => $line,
              'menu_drop'   => $this->menu,
              'menu'        => "outlet",
              'title_table'    => "Step ".$detail[0]->name,
              'foot'    => ""
            ));
      $this->template
        ->set_layout('timeline')
        ->build('timeline/report');
    }
    else if($edit !== FALSE OR $this->session->userdata("id") == 1){
      $this->template->build('timeline/start-date', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Sign Start Date',
              'id_outlet'  => $id,
              'dev'    => $dev,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('timeline/start-date');
    }
    else{
      redirect($this->session->userdata('dashbord'));
    }
  }
  public function add_sub2_step(){
    $nomor = explode("|",$_POST['no']);
    $no = $nomor[1];
    $datenya = $this->form_eksternal->form_date("sub2_tanggal_{$nomor[0]}[]", '', "sub2_tanggal_{$nomor[0]}_".$no, 'class="span10"');
    $note = "";
//    $note = $this->form_eksternal->form_textarea('sub_note[]', '', 'class="span9"');
    $pic = $this->form_eksternal->form_autocomplate("sub2_pic_view_{$nomor[0]}[]", "sub2_pic_desk_{$nomor[0]}[]", 
                              "id_sub2_pic_view_{$nomor[0]}_{$no}", "id_sub2_pic_desk_{$nomor[0]}_".$no, site_url("dev-checklist/pic-users"), 
                              "", 0, "class='span8'",
                              ""
                              );
    $step = $this->form_eksternal->form_input("sub2_step_{$nomor[0]}[]", $value = '', $extra = '');
    $check = $this->form_eksternal->form_checkbox("sub2_check_{$nomor[0]}[]", 1);
    print <<<EOD
    <tr>
        <td></td>
        <td></td>
        <td width="10"></td>
        <td>{$step}</td>
        <td>{$datenya}</td>
        <td>{$pic}</td>
        <td>{$check} Check</td>
        <td>{$note}</td>
    </tr>
EOD;
    die;
  }
  public function add_sub_step(){
    $nomor = explode("|",$_POST['no']);
    $no = $nomor[1];
    $link = site_url("dev-checklist/add-sub2-step");
    
    $datenya = $this->form_eksternal->form_date("sub_tanggal_{$nomor[0]}[]", '', "sub_tanggal_{$nomor[0]}_".$no, 'class="span10"');
    $note = "";
//    $note = $this->form_eksternal->form_textarea('sub_note[]', '', 'class="span9"');
    $pic = $this->form_eksternal->form_autocomplate("sub_pic_view_{$nomor[0]}[]", "sub_pic_desk_{$nomor[0]}[]", 
                              "id_sub_pic_view_{$nomor[0]}_{$no}", "id_sub_pic_desk_{$nomor[0]}_".$no, site_url("dev-checklist/pic-users"), 
                              "", 0, "class='span8'",
                              ""
                              );
    $step = $this->form_eksternal->form_input("sub_step_{$nomor[0]}[]", $value = '', $extra = '');
    $check = $this->form_eksternal->form_checkbox("sub_check_{$nomor[0]}[]", 1);
    
    print <<<EOD
    <tr>
        <td></td>
        <td width="20"><div style="display: none" id="sub2_param_{$nomor[0]}T{$no}">{$nomor[0]}T{$no}|1</div><a href="#" onclick="add_sub('table_step', '{$link}', 'sub_tr_{$nomor[0]}T{$no}', 'sub2_param_{$nomor[0]}T{$no}')" class="add-element" title="Add">Add</a></td>
        <td colspan="2">{$step}</td>
        <td>{$datenya}</td>
        <td>{$pic}</td>
        <td>{$check} Check</td>
        <td>{$note}</td>
    </tr>
    <tr id="sub_tr_{$nomor[0]}T{$no}"><td colspan='8'></td><tr>
EOD;
    die;
  }
  function pic_users(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
// remove slashes if they were magically added
    if (get_magic_quotes_gpc()) $q = stripslashes($q);

    $items = $this->mdev_checklist->get_user_auto($q);
    $result = array();
    foreach ($items as $key=>$value) {
      if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
      }
      if (count($result) > 11)
        break;
    }

// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
    echo json_encode($result);
    die;
  }
  public function add_grand_step(){
//    (, , "user_hidden", site_url("outlet/add_row"), "mul_users", "add_users", "nomor", "user", , "", "")
    $no = $_POST['no'];
    $link = site_url("dev-checklist/add-sub-step");
    
    $datenya = $this->form_eksternal->form_date('tanggal[]', '', 'tanggal_'.$no, 'class="span10"');
    $note = "";
//    $note = $this->form_eksternal->form_textarea('note[]', '', 'class="span9"');
    $pic = $this->form_eksternal->form_autocomplate("pic_view[]", "pic_desk[]", 
                              "id_pic_view_{$no}", "id_pic_desk_".$no, site_url("dev-checklist/pic-users"), 
                              "", 0, "class='span8'",
                              ""
                              );
    $options = $this->global_models->get_dropdown("m_dev_checklist", "id_dev_step", "name");
    $step = $this->form_eksternal->form_dropdown('step[]', $options, array());
    $check = $this->form_eksternal->form_checkbox('check[]', 1);
    
    print <<<EOD
    <tr>
        <td width="20"><div style="display: none" id="sub_param_{$no}">{$no}|1</div><a href="#" onclick="add_sub('table_step', '{$link}', 'tr_{$no}', 'sub_param_{$no}')" class="add-element" title="Add">Add</a></td>
        <td colspan="3">{$step}</td>
        <td>{$datenya}</td>
        <td>{$pic}</td>
        <td>{$check} Check</td>
        <td>{$note}</td>
    </tr>
    <tr id="tr_{$no}"><td colspan='8'></td><tr>
EOD;
    die;
  }
  function reportpost(){
    $sifat = 0;
    if($_POST['sifat'] == 'true' OR $_POST['sifat'] == 'TRUE' OR $_POST['sifat'] == '1'){
      $sifat = 1;
    }
    $kirim = array(
        "note" => $_POST['note'],
        "time_start" => $_POST['time_start'],
        "time_end" => $_POST['time_end'],
        "id_dev_step" => $_POST['section_id'],
        "id_outlet_dev" => $_POST['id_outlet_dev'],
        "sifat" => $sifat,
    );
    if($_POST['id_dev_check'] > 0)
      print $this->global_models->update("d_dev_check", array('id_dev_check' => $_POST['id_dev_check']), $kirim);
    else{
      print $this->global_models->insert("d_dev_check", $kirim);
    }
//    print "kamu satu";
    die;
  }
  function reportupdate(){
//    $this->debug($_POST);
//    $sifat = 1;
//    if($_POST['sifat'] == 'false'){
//      $sifat = 0;
//    }
    $kirim = array(
        "note" => $_POST['note'],
        "time_start" => $_POST['time_start'],
        "time_end" => $_POST['time_end'],
        "id_dev_step" => $_POST['section_id'],
        "id_outlet_dev" => $_POST['id_outlet_dev'],
        "sifat" => $_POST['sifat'],
    );
    $this->global_models->update("d_dev_check", array("id_dev_check" => $_POST['id_garis']), $kirim);
    print $_POST['id_garis'];
    die;
  }
  function reportdelete(){
    $this->global_models->delete("d_dev_check", array("id_dev_check" => $_POST['id_garis']));
    die;
  }
  function create_report(){
    $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
    if(!$pst['id_detail']){
      $kirim = array(
          "start_tanggal"         =>  $pst['start_date'],
          "max_date"              =>  $pst['max_date'],
          "id_outlet"             =>  $pst['id_outlet'],
          "create_by_users"       =>  $this->session->userdata('id'),
          "create_date"           =>  date("Y-m-d H:i:s"),
          "update_by_users"       =>  $this->session->userdata('id')
      );
      if($this->global_models->insert("d_outlet_dev", $kirim))
        redirect ("dev_checklist/report/".$pst['id_outlet']);
      else{
        redirect ("dev_checklist/report/".$pst['id_outlet']);
      }
    }
    else{
      $kirim = array(
          "start_tanggal"         =>  $pst['start_date'],
          "max_date"              =>  $pst['max_date'],
          "id_outlet"             =>  $pst['id_outlet'],
          "update_by_users"       =>  $this->session->userdata('id')
      );
      if($this->global_models->update("d_outlet_dev", array("id_outlet_dev" => $pst['id_detail']), $kirim))
        redirect ("dev_checklist/report/".$pst['id_outlet']);
      else{
        redirect ("dev_checklist/report/".$pst['id_outlet']);
      }
    }
  }
  function check_time($id_outlet){
    
//    $data = $this->global_models->get_query("SELECT * FROM d_checklist_fase ORDER BY sort ASC");
    $fase = $this->global_models->get("d_checklist_fase");
//    $this->debug($hasil, true);
    $this->template->build("aktifitas-detail", 
      array(
            'message'       => "",
            'url'           => base_url()."themes/srabon/",
            'title_table'   => "DEVELOPMENT OUTLET ".$this->global_models->get_field("m_outlet", "name", array("id_outlet" => $id_outlet)),
            'foot'          => "",
            'id_outlet'     => $id_outlet,
            'fase'          => $fase
          ));
    $this->template
      ->set_layout('default')
      ->build("aktifitas-detail");
  }
  function master_checklist(){
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM d_checklist_keterangan AS A
      LEFT JOIN d_checklist_fase AS B ON A.id_checklist_fase = B.id_checklist_fase
      
      ORDER BY B.sort ASC
      ");
    $this->template->build('master-checklist', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'title_table' => "Master Checklist",
            'data'        => $data,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master-checklist');
  }
  function master_fase(){
    $data = $this->global_models->get_query("
      SELECT *
      FROM d_checklist_fase
      ORDER BY sort ASC
      ");
    $this->template->build('master-fase', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'title_table' => "Master Fase",
            'data'        => $data,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('master-fase');
  }
  public function add_new_fase($id = 0, $pesan = "hal"){
    if($id > 0){
      $data_detail = $this->global_models->get("d_checklist_fase", array("id_checklist_fase" => $id));
//      $this->debug($data_detail, true);
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-new-fase', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Fase',
              'detail'  => $data_detail,
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-fase');
    }
    else{
      $pst = $this->input->post(NULL);
      if(!$pst['id_detail']){
        $kirim = array(
            'name'          => $pst['name'],
            'sort'          => $pst['sort'],
            'urut'          => $pst['urut'],
            'create_by_users' => $this->session->userdata('id'),
            'create_date'   => date("Y-m-d")
        );
        if($this->global_models->insert("d_checklist_fase", $kirim))
          redirect ("dev-checklist/master-fase/1/Berhasil");
        else
          redirect ("dev-checklist/master-fase/2/Gagal");
      }
      else{
        $kirim = array(
            'name'          => $pst['name'],
            'sort'          => $pst['sort'],
            'urut'          => $pst['urut'],
            'update_by_users' => $this->session->userdata('id'),
        );
        if($this->global_models->update("d_checklist_fase", array("id_checklist_fase" => $pst['id_detail']), $kirim))
          redirect ("dev-checklist/master-fase/1/Berhasil");
        else
          redirect ("dev-checklist/master-fase/2/Gagal");
      }
    }
  }
  public function add_new_keterangan($id = 0, $pesan = "hal"){
    if($id > 0){
      $data_detail = $this->global_models->get("d_checklist_keterangan", array("id_checklist_keterangan" => $id));
//      $this->debug($data_detail, true);
    }
    if(!$this->input->post(NULL, TRUE)){
      $fase = $this->global_models->get_dropdown("d_checklist_fase", "id_checklist_fase", "name", FALSE);
      $parent = $this->global_models->get_dropdown("d_checklist_keterangan", "id_checklist_keterangan", "name", TRUE, array("parent" => 0));
//      $this->debug($parent, true);
      $this->template->build('add-new-keterangan', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Keterangan',
              'detail'  => $data_detail,
              'fase'    => $fase,
              'parent'  => $parent,
              'foot'    => ''
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-keterangan');
    }
    else{
      $pst = $this->input->post(NULL);
      if(!$pst['id_detail']){
        if(!$pst['cek'])
          $pst['cek'] = 0;
        if($pst['parent']){
          $parent = $this->global_models->get("d_checklist_keterangan", array("id_checklist_keterangan" => $pst['parent']));
          $pst['fase'] = $parent[0]->id_checklist_fase;
        }
        $kirim = array(
            'name'                => $pst['name'],
            'id_checklist_fase'   => $pst['fase'],
            'id_privilege'        => $pst['id_privilege'],
            'cek'                 => $pst['cek'],
            'parent'              => $pst['parent'],
            'sort'                => $pst['sort'],
            'level1'              => $pst['sort'],
            'level2'              => 0,
            'level3'              => 0,
            'level4'              => 0,
            'level5'              => 0,
            'level6'              => 0,
            'create_by_users'     => $this->session->userdata('id'),
            'create_date'         => date("Y-m-d")
        );
        
        if($pst['level1']){
          $kirim['level1']    = $pst['id_level1'];
          $kirim['level2']    = $pst['sort'];
          if($pst['level2']){
            $kirim['level2']    = $pst['id_level2'];
            $kirim['level3']    = $pst['sort'];
            if($pst['level3']){
              $kirim['level3']    = $pst['id_level3'];
              $kirim['level4']    = $pst['sort'];
              if($pst['level4']){
                $kirim['level4']    = $pst['id_level4'];
                $kirim['level5']    = $pst['sort'];
                if($pst['level5']){
                  $kirim['level5']    = $pst['id_level5'];
                  $kirim['level6']    = $pst['sort'];
                }
              }
            }
          }
        }
        
        if($this->global_models->insert("d_checklist_keterangan", $kirim))
          redirect ("dev-checklist/master-checklist/1/Berhasil");
        else
          redirect ("dev-checklist/master-checklist/2/Gagal");
      }
      else{
        $kirim = array(
            'name'                => $pst['name'],
            'id_checklist_fase'   => $pst['fase'],
            'id_privilege'        => $pst['id_privilege'],
            'cek'                 => $pst['cek'],
            'parent'              => $pst['parent'],
            'sort'                => $pst['sort'],
            'level1'              => $pst['sort'],
            'level2'              => 0,
            'level3'              => 0,
            'level4'              => 0,
            'level5'              => 0,
            'level6'              => 0,
            'create_by_users'     => $this->session->userdata('id'),
            'create_date'         => date("Y-m-d")
        );
        
        if($pst['level1']){
          $kirim['level1']    = $pst['id_level1'];
          $kirim['level2']    = $pst['sort'];
          if($pst['level2']){
            $kirim['level2']    = $pst['id_level2'];
            $kirim['level3']    = $pst['sort'];
            if($pst['level3']){
              $kirim['level3']    = $pst['id_level3'];
              $kirim['level4']    = $pst['sort'];
              if($pst['level4']){
                $kirim['level4']    = $pst['id_level4'];
                $kirim['level5']    = $pst['sort'];
                if($pst['level5']){
                  $kirim['level5']    = $pst['id_level5'];
                  $kirim['level6']    = $pst['sort'];
                }
              }
            }
          }
        }
        
        if($this->global_models->update("d_checklist_keterangan", array("id_checklist_keterangan" => $pst['id_detail']), $kirim))
          redirect ("dev-checklist/master-checklist/1/Berhasil");
        else
          redirect ("dev-checklist/master-checklist/2/Gagal");
      }
    }
  }
  function autoprivilege(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->mdev_checklist->get_auto_privilege($q);
    $result = array();
    foreach ($items as $key=>$value) {
      if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
      }
      if (count($result) > 11)
        break;
    }
    echo json_encode($result);
    die;
  }
  function autolevel($level, $fase, $level1 = 0, $level2 = 0, $level3 = 0){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->mdev_checklist->get_auto_level($fase, $q, $level1, $level2, $level3);
    $result = array();
    foreach ($items as $key=>$value) {
      if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
      }
      if (count($result) > 11)
        break;
    }
    echo json_encode($result);
    die;
  }
  function ajax_cek(){
    $pst = $this->input->post(NULL);
    if($pst['tanggal'])
      $tanggal = $pst['tanggal'];
    else
      $tanggal = date("Y-m-d");
    $this->global_models->delete("d_checklist_outlet", array(
        "id_checklist_keterangan" => $pst['id'],
        "id_outlet" => $pst['id_outlet'],
    ));
    $this->global_models->insert("d_checklist_outlet", array(
        "kondisi" =>  $pst['kondisi'], 
        "id_checklist_keterangan" => $pst['id'],
        "id_outlet" => $pst['id_outlet'],
        "tanggal" => $tanggal,
        "create_by_users" => $this->session->userdata('id'),
        "create_date" => date("Y-m-d")
        ));
    die;
  }
  public function daftar_outlet($id = 0){
    if($id <= 0){
      if($this->session->userdata("id") > 0)
        $id = $this->session->userdata("id");
    }
    $list = $this->mdev_checklist->get_milik($id);
    $this->template->title('Sistem', "Outlet");
    
    $view = "milik";
    if($this->mobile_detect->isMobile()){
      $view = "mobile/m-milik";
    }
    
    $this->template->build($view, 
      array('message' => "",
            'url'     => base_url()."themes/srabon/",
            'data'    => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table'    => "Outlet In Development",
            'foot'    => ""
          ));
    $this->template
      ->set_layout('default')
      ->build($view);
  }
  function ajax_get_data(){
    $id = $this->input->post("id");
    $id_outlet = $this->input->post("id_outlet");
    $keputusan_cek = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "dev_checklist_field", "edit");
//    $keputusan_cek = TRUE;
    $cetak_anak = "
      <table id='ajax' class='ajax table post-tbl table-striped'>
    ";
    $anak = $this->global_models->get_query("
      SELECT A.*, B.kondisi AS b_kondisi, B.tanggal AS b_tanggal
      FROM d_checklist_keterangan AS A
      LEFT JOIN d_checklist_outlet AS B ON (A.id_checklist_keterangan = B.id_checklist_keterangan AND B.id_outlet ={$id_outlet})
      WHERE A.id_checklist_fase = {$id}
      ORDER BY A.level1, A.level2, A.level3, A.level4, A.level5, A.level6 ASC
      ");
//    $cetak_anak = "";
    $class_urut = 0;
    $posisi_buka = 0;
    $first_step = "";
    $first_date = "";
    $last_step = "";
    $last_date = "";
    foreach($anak as $ank){
      $bayi = "<b>{$ank->level1}. {$ank->name}</b>";
      if($ank->level2 > 0)
        $bayi = "<div style='margin-left: 20px;float: left'>{$ank->level2}. </div><div style='margin-left: 30px;'> {$ank->name}</div>";
      if($ank->level3 > 0)
        $bayi = "<div style='margin-left: 45px;float: left'>{$ank->level3}. </div><div style='margin-left: 55px;'>{$ank->name}</div>";
      if($ank->level4 > 0)
        $bayi = "<div style='margin-left: 70px;float: left'>{$ank->level4}. </div><div style='margin-left: 80px;'> {$ank->name}</div>";
      if($ank->level5 > 0)
        $bayi = "<div style='margin-left: 95px;float: left'>{$ank->level5}. </div><div style='margin-left: 105px;'> {$ank->name}</div>";
      if($ank->level6 > 0)
        $bayi = "<div style='margin-left: 120px;float: left'>{$ank->level6}. </div><div style='margin-left: 130px;'> {$ank->name}</div>";
      $show = "none";  
      $pengecek = "";
      if($ank->cek == 1){
        if($fs->urut == 1){
          $kondisi_cek = "style='display: none'";
        }
        else
          $kondisi_cek = "";
        if($ank->b_kondisi == 1){
          if(!$first_step){
            $first_step = $ank->name;
            $first_date = $ank->b_tanggal;
          }
          if($ank->b_tanggal > $last_date){
            $last_step = $ank->name;
            $last_date = $ank->b_tanggal;
          }
          $true = TRUE;
          $show = "block";
          $kondisi_cek = "";
          $posisi_buka++;
        }
        else{
          $true = FALSE;
          $show = "none";
          if($class_urut == $posisi_buka){
            $kondisi_cek = "";
          }
        }

        $pengecek = $this->form_eksternal->form_checkbox('cek_'.$ank->id_checklist_keterangan, '', $true, "onclick='simpan_cek({$ank->id_checklist_keterangan}, {$id}, $class_urut)' id='cekbok{$ank->id_checklist_keterangan}' {$kondisi_cek} class='urut_{$fs->id_checklist_fase}_{$class_urut}'", $keputusan_cek);
        $class_urut++;
      }
      $cetak_anak .= "
        <tr>
          <td>{$bayi}</td>
          <td>{$pengecek}</td>
          <td>{$this->form_eksternal->form_date("tanggal_".$ank->id_checklist_keterangan, $ank->b_tanggal, "tanggal".$ank->id_checklist_keterangan, "class='input-small' style='display: {$show};'",
                     "onSelect: function(dateText) {
                            $.ajax({
                              type: 'POST',
                              url: '".  site_url("dev-checklist/ajax-cek")."',
                              data: { id: {$ank->id_checklist_keterangan}, kondisi: 1, tanggal: dateText, id_outlet: {$id_outlet} }
                            }).done(function( msg ) {
                            });
                          },"
                     , $keputusan_cek)}</td>
        </tr>
        ";
    }
    $cetak_anak .= "</table>";
    print "
      <table class='ajax table post-tbl'>
        <tr>
          <td width='100'><b>First Check</b></td>
          <td width='1'>: </td>
          <td>{$first_step}</td>
        </tr>
        <tr>
          <td><b>First Check Date</b></td>
          <td>: </td>
          <td>{$first_date}</td>
        </tr>
        <tr>
          <td><b>Last Check</b></td>
          <td>: </td>
          <td>{$last_step}</td>
        </tr>
        <tr>
          <td><b>Last Check Date</b></td>
          <td>: </td>
          <td>{$last_date}</td>
        </tr>
        <tr>
          <td colspan='3'><hr /></td>
        </tr>
      </table>
    ";
    print $cetak_anak;
    die;
  }
  
  public function time_line_new($id_outlet) {
    $dev = $this->global_models->get("d_outlet_dev", array("id_outlet" => $id_outlet));
    $outlet = $this->global_models->get("m_outlet", array("id_outlet" => $id_outlet));
//    $query = "
//      SELECT A.*
//      FROM m_dev_checklist AS A
//      ORDER BY A.urut ASC
//      ";
//    $fase = $this->global_models->get_query($query);
    $query = "
      SELECT D.*
      FROM `d_outlet_users` as A
      LEFT JOIN d_user_privilege as B ON A.id_users = B.id_users
      LEFT JOIN m_privilege as C ON B.id_privilege = C.id_privilege
      LEFT JOIN m_users as D ON A.id_users = D.id_users
      WHERE A.id_outlet = 5 AND LOWER(C.name) LIKE '%owner outlet%'
      ";
    $owner = $this->global_models->get_query($query);
    $edit = $this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "dev_timeline", "edit");
    $edit = TRUE;
    $this->template->build("time-line-new",
      array('message'     => urldecode($pesan),
            'url'         => base_url()."themes/timeline/",
            'theme2nd'    =>  'timeline',
            'dev'         =>  $dev,
            'edit'        =>  $edit,
            'detail'      => $outlet,
            'owner'       => $owner,
//            'fase'        =>  $fase,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build("time-line-new");
  }
  function get_data_fase(){
    $pst = $this->input->post(NULL);
    $hasil = array(
        array(
            "name" => "Development",
            "desc" => "Time Line",
            "values" => array(
                array(
                  "from" => "/Date({$pst['dev_start']})/",
                  "to" => "/Date({$pst['dev_end']})/",
                  "label" => "Development Time Line", 
                  "customClass" => "ganttOrange"
                )
            )
        )
    );
                  
    $query = "
      SELECT A.*
      FROM m_dev_checklist AS A
      ORDER BY A.urut ASC
    ";
    $fase = $this->global_models->get_query($query);
    foreach($fase as $fs){
      $dtarget = array();
      $dactual = array();
      $dtarget_master = $this->global_models->get("d_dev_check", array("id_outlet_dev" => $pst['id_outlet_dev'], "id_dev_step" => $fs->id_dev_step, "sifat" => 1));
      if($dtarget_master){
        $start_dari = strtotime($dtarget_master[0]->time_start." +1 day");
        $end_dari = strtotime($dtarget_master[0]->time_end." +1 day");
        $dtarget = array(
            array(
                "from"  => "/Date({$start_dari}000)/",
                "to" => "/Date({$end_dari}000)/",
                "label" => $dtarget_master[0]->note, 
                "customClass" => 'ganttRed',
                "dataObj" => $dtarget_master[0]->id_dev_check
            )
        );
      }
      
      $dactual_master = $this->global_models->get("d_dev_check", array("id_outlet_dev" => $pst['id_outlet_dev'], "id_dev_step" => $fs->id_dev_step, "sifat" => 0));
      if($dactual_master){
        $start_dari = strtotime($dactual_master[0]->time_start." +1 day");
        $end_dari = strtotime($dactual_master[0]->time_end." +1 day");
        $dactual = array(
            array(
                "from"  => "/Date({$start_dari}000)/",
                "to" => "/Date({$end_dari}000)/",
                "label" => $dactual_master[0]->note, 
                "customClass" => 'ganttBlue',
                "dataObj" => $dactual_master[0]->id_dev_check
            )
        );
      }
      
      $hasil[] = array(
        "name" => $fs->name,
        "desc" => "Target",
        "id" => $fs->id_dev_step.'2',
        "values" => $dtarget
      );
      $hasil[] = array(
        "name" => "",
        "desc" => "Actual",
        "id" => $fs->id_dev_step.'1',
        "values" => $dactual
      );
    }
    
    print json_encode($hasil);
    die;
  }
  private function lost_jam($date){
    $hasil = explode(" ", $date);
    return $hasil[0];
  }
  function get_data_dev_check_detail(){
    $pst = $this->input->post(NULL);
    $master = $this->global_models->get("d_dev_check", array("id_dev_check" => $pst['id']));
    $hasil = array(
        'id_dev_check'    =>  $master[0]->id_dev_check,
        'id_dev_step'     =>  $master[0]->id_dev_step,
        'note'            =>  $master[0]->note,
        'sifat'           =>  $master[0]->sifat,
        'time_start'      =>  $this->lost_jam($master[0]->time_start),
        'time_end'        =>  $this->lost_jam($master[0]->time_end)
    );
    print json_encode($hasil);
    die;
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */